import React, {Component} from 'react'
import {fetchData} from '../../../components/AjaxConsumer'
import {
    Form,
    Input,
    Tooltip,
    Icon,
    Cascader,
    Select,
    Row,
    Col,
    Checkbox,
    Button,
    message,
    Modal,
    Radio,
    InputNumber
} from 'antd';
import { SelectUrl } from '../../../components';
// import ModelModalTable from './ModelModalTable'
const FormItem = Form.Item;

class WorkAreaForm extends React.Component {
    state = {
        loading: false,
        isEdit: false,
        submitUrl: '',
        // unitLengthUrl:'/base/dic/get/unit_length',
        formData: {}
    };

    // onWhCodeChange = (whCode) => {
    //     if (whCode != '' && whCode != undefined) {
    //         this.setState({
    //             regionCodeUrl: '/base/region/r/' + whCode + '/region',
    //         });
    //     }
    // }

    // modelModalTableSearchClick = () => {
    //     this.refs.modelModalTable.showModal();
    // }

    // 型号选择弹出模态框的统一回调函数
    // getModelTableData = (resultData) => {
    //     // if(resultData.type=='instock'){
    //     this.props.form.setFieldsValue(
    //         {
    //             model: resultData.model,
    //             modelType: resultData.modelType,
    //             modelDesc: resultData.modelDescZh,
    //             unit: resultData.unit
    //         }
    //     );
    //     // }

    // }

    handleSubmit = (e) => {
        e.preventDefault();
        console.log('44444444444-----------');
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {
                this.setState({loading: true});
                let values = {
                    ...fieldsValue,
                };
                console.log('表单提交数据-----------', values);
                //发送ajax 控制请求数据规则，无值的设为空
                for (let item in values) {
                    if (!values[item]) {
                        values[item] = ''
                    }
                }
                const options = {
                    url: this.state.submitUrl,
                    type: 'post',
                    condition: values
                };

                fetchData(
                    this,
                    options,
                    (res) => {
                        //成功回调函数
                        this.setState({
                            loading: false
                        });
                        if (res.code == '1') {
                            message.success('操作成功');

                            this.handleCancel();
                            this.props.refreshHandle();
                        } else {
                            //message.error(res.msg);
                        }

                    },
                    (res) => {
                        this.setState({
                            loading: false
                        });
                    }
                )
            }
        });
    }

    handleCancel = () => {
        this.props.onCancel();//123
        this.props.form.resetFields(); //初始化表单
        //初始化ajax加载过来的selectList
    }

//     checkOutAmountNumber = (rule, value, callback) => {
//     const form = this.props.form;
//     if (value && value>0) {
//       callback();
//     } else {
//       callback('基本单位数量应为大于0的数字');
//     }
//   }

//     checkInAmountNumber = (rule, value, callback) => {
//     const form = this.props.form;
//     if (value && value>0) {
//       callback();
//     } else {
//       callback('转换数量应为大于0的数字');
//     }
//   }

    //propertity 修改时执行
    componentWillReceiveProps(newProps){
        this.setState({
            isEdit: newProps.isEdit,
            submitUrl: newProps.submitUrl,
            formData: newProps.formData
        });
    }

    // 组件参数实例化-仅一次
    componentWillMount(){
        this.setState({
            isEdit: this.props.isEdit,
            submitUrl: this.props.submitUrl,
            formData: this.props.formData
        });
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6}
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 18}
            }
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 24, offset: 10}
            }
        };

        let objectInfo = this.state.formData;

        return (
            <div>
                <Form
                    onSubmit={this.handleSubmit}
                    style={{
                                backgroundColor: '#fff',
                                paddingTop: '20px',
                                paddingBottom: '20px'
                            }}>
                    <Row>
                        <Col span={0}>
                            <FormItem>
                                {getFieldDecorator('purchaseId', {
                                    initialValue: objectInfo.purchaseId ? objectInfo.purchaseId.toString() : "",
                                    rules: []
                                })(<Input type="hidden"/>)}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="货主编码">
                                {getFieldDecorator('purchaseCode', {
                                    initialValue: objectInfo.purchaseCode ? objectInfo.purchaseCode : "",
                                    rules: [
                                        // {pattern:/^[a-zA-Z0-9]*$/,message:'输入内容不合法'}
                                    ]
                                })(<Input disabled/>)}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="货主名称">
                                {getFieldDecorator('purchaseName', {
                                    initialValue: objectInfo.purchaseName ? objectInfo.purchaseName : "",
                                    rules: [
                                    ]
                                })(
                                    <Input type="text"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="状态">
                                {getFieldDecorator('activeFlag', {
                                    initialValue: objectInfo.activeFlag ? objectInfo.activeFlag : "",
                                    rules: [
                                    ]
                                })(
                                    <Select>
                                        <Select.Option value='1'>启用</Select.Option>
                                        <Select.Option value='2'>禁用</Select.Option>
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24} style={{ textAlign: 'center',marginTop:'10px' }}>
                            <Button type="primary" htmlType="submit" size="large">
                                {this.state.loading ? '提交中...' : '确定'}
                            </Button>
                            <Button type="default" style={{marginLeft: '50px'}} size="large" onClick={this.handleCancel}>
                                取消
                            </Button>
                        </Col>
                    </Row>
                </Form>

                {/* <ModelModalTable getTableData={this.getModelTableData} ref='modelModalTable' /> */}
            </div>
        );
    }
}

export default Form.create()(WorkAreaForm);
